package com.time.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.time.domain.ProductCategory;
import com.time.mapper.ProductCategoryMapper;
import com.time.service.ProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 张龙佼
 * @Description 商品分类信息 service接口实现
 * @since 2020-10-21
 */

@Service
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategory>
        implements ProductCategoryService {

    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    @Override
    public List<ProductCategory> productCategories() {
        return productCategoryMapper.productCategories();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addProductCategory(ProductCategory productCategory) {
        int addProductCategory = productCategoryMapper.insert(productCategory);

        // 若商品父id为0，则添加父分类
        int updateProductCategory = 0;
        if (productCategory.getParentId() == 0) {
            productCategory.setParentId(productCategory.getId());
            updateProductCategory = productCategoryMapper.updateById(productCategory);
        }

        return addProductCategory > 0 && updateProductCategory > 0;
    }

    @Override
    public boolean updateProductCategory(ProductCategory productCategory) {
        // 若父id为0，则为更新父分类，将当前id赋值给父id
        if (productCategory.getParentId() == 0) {
            productCategory.setParentId(productCategory.getId());
        }

        return productCategoryMapper.updateById(productCategory) > 0;
    }
}
